package com.beiing.tianshuai.tianshuai.huodong.model;

import com.beiing.tianshuai.tianshuai.entity.ActivitiesApplyForResultBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;

/**
 * 提交商品信息的model层
 *
 * @author Jinda Yang
 * @date 2018/5/20
 */
public class ActivitiesApplyForModel implements ActivitiesApplyForModelImpl {

    private static final String TAG = "ActivitiesApplyForModel";
    private OnResponseListener mResponseListener;

    public ActivitiesApplyForModel(OnResponseListener responseListener) {
        mResponseListener = responseListener;
    }

    @Override
    public void submitForFreeTicket(String uid, String aid, String amount, String tel, String username) {
        MultipartBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("aid", aid)
                .addFormDataPart("uid", uid)
                .addFormDataPart("count", amount)
                .addFormDataPart("tel", tel)
                .addFormDataPart("uname", username)
                .build();
        DataManager
                .getSubmitForFreeTicketResult(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<ActivitiesApplyForResultBean>() {
                    @Override
                    public void onNext(ActivitiesApplyForResultBean bean) {
                        if (mResponseListener != null) {
                            mResponseListener.onSubmitFreeActivitySuccess(bean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mResponseListener != null) {
                            mResponseListener.onSubmitFreeActivityError(e);
                        }
                    }

                    @Override
                    public void onComplete() {
                        LogUtils.i("==== 提交免费活动报名信息结束 ====", TAG);
                    }
                });
    }

    public interface OnResponseListener {

        /**
         * 提交免费活动报名信息成功
         *
         * @param bean 免费活动报名请求结果
         */
        void onSubmitFreeActivitySuccess(ActivitiesApplyForResultBean bean);

        /**
         * 提交免费活动报名信息失败
         *
         * @param throwable 免费活动报名请求异常信息
         */
        void onSubmitFreeActivityError(Throwable throwable);
    }
}
